<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>06_员工列表练习</title>
</head>
<body>
<input type="text" placeholder="请输入员工姓名" id="i1">
<input type="text" placeholder="请输入员工工资" id="i2">
<input type="text" placeholder="请输入员工岗位" id="i3">
<button onclick="addEmp()">添加员工</button>
<hr>
<table border="1px">
    <caption>员工表</caption>
    <tr>
        <th>姓名</th>
        <th>工资</th>
        <th>岗位</th>
    </tr>
</table>
<script>
    //8.准备一个空的员工数据,用于后端新增
    let arr = [];
    //1.定义添加员工的方法
    function addEmp() {
        //2.收集员工数据
        let uName = document.getElementById('i1').value;
        let uSalary = document.getElementById('i2').value;
        let uJob = document.getElementById('i3').value;
        //3.每多一个员工,就要新建1个tr与3个td
        let tr = document.createElement('tr');
        let nameTd = document.createElement('td');
        let salaryTd = document.createElement('td');
        let jobTd = document.createElement('td');
        //4.将收集到的数据装入单元格里
        nameTd.innerHTML = uName;
        salaryTd.innerHTML = uSalary;
        jobTd.innerHTML = uJob;
        //5.将准备好的3个td装入tr中
        tr.append(nameTd, salaryTd, jobTd);
        //6.获取上方的表格元素,并将准备好的tr装入表格中
        let table = document.querySelector('table');
        table.append(tr);
        //7.用完的数据要清空
        document.getElementById('i1').value = '';
        document.getElementById('i2').value = '';
        document.getElementById('i3').value = '';
        //9.向数组中存入当前员工数据
        arr.push({
            name:uName,
            salary:uSalary,
            job: uJob
        });
        console.log(arr);
    }
</script>
</body>
</html>